package com.ermax.aircond.common.web.bean;

import java.util.List;

import javax.annotation.Resource;
import javax.faces.context.FacesContext;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ermax.aircond.common.domain.ReadMachineData;
import com.ermax.aircond.common.service.ReadMachineDataService;
import com.ermax.aircond.common.service.WriteMachineDataService;
import com.ermax.aircond.report.domain.CommandCheck;
import com.ermax.aircond.resource.MessageBundle;


@Component
@Scope("session")
public class AlertBean implements java.io.Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;	
		
	@Resource(name="readMachineDataService")
	private ReadMachineDataService readMachineDataService;
	
	@Resource(name = "writeMachineDataService")
	private WriteMachineDataService writeMachineDataService;
			
	public String getAlertMessage(){
			
		//Read from DB
		List<ReadMachineData> list = readMachineDataService.getErrorNidData("E3");
		if(list.size() > 0){					
			StringBuilder sb = new StringBuilder();
			sb.append(MessageBundle.getInstance().getMessage("error_machine_nid", FacesContext.getCurrentInstance().getViewRoot().getLocale())); 
			sb.append(": ");
			
			for(int i=0; i< list.size(); i++){
				ReadMachineData readMachineData = list.get(i);
				sb.append(readMachineData.getNid());
				if(i == 5){
					sb.append("...");
					break;
				}else{
					if(i != list.size() -1){
						sb.append(",");
					}
				}
			}
			return sb.toString();
		}
		
		
		return "";
	}
	
	public String getIneffectiveCommand() {
		
		List<CommandCheck> commandCheckData = writeMachineDataService.findLatestMachineData();
		
		if(commandCheckData != null && commandCheckData.size() > 0){					
			StringBuilder sb = new StringBuilder();
			sb.append(MessageBundle.getInstance().getMessage("ineffective_machine_nid", FacesContext.getCurrentInstance().getViewRoot().getLocale())); 
			sb.append(": ");
			for (int i = 0; i < commandCheckData.size(); i++) {
				CommandCheck commandCheck = commandCheckData.get(i);
				sb.append(commandCheck.getNid());
				if(i == 5){
					sb.append("...");
					break;
				}else{
					if(i != commandCheckData.size() -1){
						sb.append(",");
					}
				}
			}
			return sb.toString();
		}
		
		return "";
	}
}
